package com.zheng.shop.manage.controller.manage;

import com.zheng.shop.manage.dao.model.TbItem;
import com.zheng.shop.manage.dao.model.TbItemExample;
import com.zheng.shop.manage.rpc.api.TbItemService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RequestMapping("/manage/item")
@Controller
public class TbItemController {

    private static final Logger LOGGER = LoggerFactory.getLogger(TbItemController.class);

    @Autowired
    private TbItemService tbItemService;

    @ApiOperation(value = "评论首页")
    @RequiresPermissions("shop-manage:item:read")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/manage/item/index.jsp";
    }

    @ApiOperation(value = "商品列表")
    @RequiresPermissions("shop-manage:item:read")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
        TbItemExample tbItemExample = new TbItemExample();
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            tbItemExample.setOrderByClause(sort + " " + order);
        }
        List<TbItem> rows = tbItemService.selectByExampleForOffsetPage(tbItemExample, offset, limit);
        long total = tbItemService.countByExample(tbItemExample);
        Map<String, Object> result = new HashMap<>(2);
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }
}
